#include "args.h"
#include "time.h"

// From p2.pdf
void help() {
    printf("-h  |--help              print this help message\n");
    printf("--log                    filename write the logs in filename (default: onecard.log) -n\n");
    printf("-n n|--player-number=n   n players, n must be larger than 2 (default: 4) -c\n");
    printf("-c c|--initial-cards=c   deal c cards per player, c must be at least 2 (default: 5) -d\n");
    printf("-d d|--decks=d           use d decks 52 cards each, d must be at least 2 (default: 2) -r\n");
    printf("-r r|--rounds=r          play r rounds, r must be at least 1 (default: 1) \n");
    printf("-a  |--auto              run in demo mode\n");
#ifdef USE_GUI
    printf("-ng |--nogui             run without gui\n");
#endif
    exit(0);
}

struct args args_read(int argc, char *argv[]) {
    struct args args;
    memset(&args, 0, sizeof(args));
    args.n = 3;
    args.r = 5;
    args.c = 7;
    args.seed = time(NULL);
    args.deck_count = 1;
    args.mode = MODE_NORMAL;
    args.use_GUI = true;
    strcpy(args.log_file, "logs.txt");
    for (int i = 1; i < argc; i++) {
        if (!strcmp(argv[i], "-n"))
            args.n = atoi(argv[i + 1]);
        else if (!strncmp(argv[i], "--player-number=", 16))
            args.n = atoi(argv[i] + 16);
        else if (!strcmp(argv[i], "-r"))
            args.r = atoi(argv[i + 1]);
        else if (!strncmp(argv[i], "--rounds=", 9))
            args.r = atoi(argv[i] + 9);
        else if (!strcmp(argv[i], "-c"))
            args.c = atoi(argv[i + 1]);
        else if (!strncmp(argv[i], "--initial-cards=", 16))
            args.c = atoi(argv[i] + 16);
        else if (!strcmp(argv[i], "-d"))
            args.deck_count = atoi(argv[i + 1]);
        else if (!strncmp(argv[i], "--decks=", 8))
            args.deck_count = atoi(argv[i] + 8);
        else if (!strcmp(argv[i], "-a"))
            args.mode = MODE_DEMO;
        else if (!strcmp(argv[i], "--auto"))
            args.mode = MODE_DEMO;
        else if (!strcmp(argv[i], "-h"))
            help();
        else if (!strcmp(argv[i], "--help"))
            help();
        else if (!strcmp(argv[i], "--log"))
            strcpy(args.log_file, argv[i + 1]);
        else if (!strcmp(argv[i], "--nogui"))
            args.use_GUI = false;
        else if (!strcmp(argv[i], "-ng"))
            args.use_GUI = false;
    }
    if (strlen(args.log_file)) {
        args.log_filep = fopen(args.log_file, "w");
    }
#ifndef USE_GUI
    args.use_GUI = false;
#endif
    return args;
}
